package com.yuneec.droneservice.command;

import com.MAVLink.yuneec.msg_mav_rc_cmd_ack;
import com.yuneec.droneservice.model.GPSTime;
import com.yuneec.droneservice.request.IResponse;

/**
 * Created by gjw on 2017/3/27.
 */

public class GetGpsTime extends BaseCommand {

    public GetGpsTime() {
        super(MAV_RC_COMMAND.MAV_RC_CMD_GET_GPS_TIME);
    }

    @Override
    protected short[] body() {
        return null;
    }

    @Override
    public IResponse toResponse(msg_mav_rc_cmd_ack ack) {
        int code = ack.code;
        IResponse<GPSTime> response = new IResponse(ids(), code);
        if (code == MAV_RC_ERROR_CODE.MAV_RC_ERR_SUCCESS) {
            try {
                if (ack.params == null || ack.params.length < 7) {
                    response.setCode(MAV_RC_ERROR_CODE.MAV_RC_ERR_UNKNOW);
                    return response;
                }
                GPSTime gpsTime = new GPSTime();
                gpsTime.setYear(uIntArrayToUShort(ack.params, 0));
                gpsTime.setMonth(ack.params[2]);
                gpsTime.setDay(ack.params[3]);
                gpsTime.setHour(ack.params[4]);
                gpsTime.setMinute(ack.params[5]);
                gpsTime.setSenond(ack.params[6]);
                response.setParam(gpsTime);
            } catch (Exception e) {
                response.setCode(MAV_RC_ERROR_CODE.MAV_RC_ERR_UNKNOW);
            }
        }
        return response;
    }

}
